package com.demo.chapter10optional;

import java.util.Optional;

import com.demo.domain.Car;
import com.demo.domain.Insurance;
import com.demo.domain.Person;

public class FilterDemo {
	
	public static void demo(Optional<Insurance> insuranceOpt){
		//假如保险公司的名字是pingan，则输出
		insuranceOpt.filter(insurance -> "pingan".equals(insurance.getName())).ifPresent(System.out::print);
	}
	
	public static void demo(Person person, int minAge){
		Optional<Person> optPerson = Optional.ofNullable(person);
		String name = optPerson
			.filter(p -> p.getAge() > minAge)
			.flatMap(Person::getCarOptional)
			.flatMap(Car::getInsuranceOptional)
			.map(Insurance::getName)
			.orElse("unknow");
	}

}
